package cn.itsource.org.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.ExcelUtils;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.dto.ShopDto;
import cn.itsource.org.domain.dto.ShopRejectDto;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopRejectDto shopRejectDto){
        return shopService.pass(shopRejectDto);
    }

    @GetMapping("/activation")
    public AjaxResult activation(@RequestParam("shopId") Long shopId){
        return shopService.activation(shopId);
    }

    @GetMapping("/countRegisterTimeEcharts")
    public AjaxResult countRegisterTimeEcharts(){
        return shopService.countRegisterTimeEcharts();
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> shopList = shopService.selectAll();
        ExcelUtils.exportExcel(shopList,"店铺信息","店铺信息sheet",
                Shop.class,"导出的店铺信息.xls",response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //1 MultipartFile  文件
        //2 titleRows 你的title有几行 1
        //3 headerRows  表头
        List<Shop> shopList = ExcelUtils.importExcel(file, 1, 1, Shop.class);
        //将读到的数据保存到数据库
        for (Shop shop : shopList) {
            shopService.save(shop);
        }
    }

    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopRejectDto shopRejectDto){
        return shopService.reject(shopRejectDto);
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody ShopDto shop){
        return shopService.settlement(shop);
    }

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){ 
        if( shop.getId()!=null){
		shopService.update(shop);
        }else{
                shopService.save(shop);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        shopService.deleteById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        shopService.batchDeleteByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.selectById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.selectAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pageList")
    public AjaxResult queryPage(@RequestBody ShopQuery query) {
        return AjaxResult.me().setResultObj( shopService.queryPage(query));
    }


}
